package com.it;


import java.util.Arrays;
import java.util.Random;

public class MethodDemo {
    public static void main(String[] args) {

        say();
        eat();

        //方法调用时实际传递给方法的值就是实参
        sayHi("zg", 20);
        sayHi("yhm", 19);

        //直接调用
        System.out.println(sum(10, 20));

        //赋值调用
        int sum_num = sum(10, 20);
        System.out.println(sum_num);

        //实参传递变量也是可以的
        int a = 10;
        int b = 20;
        int sum = sum(a, b);
        System.out.println(sum);


        //随机数组的调用
        int[] arrays = arrays();//赋值调用


        //获取随机数组的第0个索引的值，第一个人访问
        System.out.println(arrays[0]);

        //打印数组,第二个人访问
        System.out.println(Arrays.toString(arrays));


        //打印数组
        int[] ints = arrays1(10, 1000);
        System.out.println(Arrays.toString(ints));

        //打印最后一个元素
        System.out.println(ints[ints.length - 1]);

        //
        say1("hello",61);
    }


    //无参无返回值
    public static void say() {
        System.out.println("hello world");
    }

    //无参无返回值
    public static void eat() {
        System.out.println("我在吃饭");
    }


    //有参无返回值
    //形参是方法定义中声明的变量
    public static void sayHi(String name, int age) {
        System.out.println("hello\t" + name + "我今年" + age + "了");
    }

    //有参有返回值
    public static int sum(int num1, int num2) {
        int i = num1 + num2;
        return i;
    }

    //创建一个长度为10的整数型数组，并随机赋值
    public static int[] arrays() {
        int[] ints = new int[10];
        Random random = new Random();
        for (int i = 0; i < ints.length; i++) {
            ints[i] = random.nextInt(101);
        }
        return ints;
    }

    //创建一个长度为10的整数型数组，并随机赋值，有参数有返回值

    //len是数组长度,max是随机数的范围
    public static int[] arrays1(int len, int max) {
        int[] ints = new int[len];
        Random random = new Random();
        for (int i = 0; i < ints.length; i++) {
            ints[i] = random.nextInt(max);
        }
        return ints;
    }

    //有参无返回值
    public static void say1(String name, int age) {
        if (age > 60) {
            return;//这里如果age>60，那么下面的打印就不会执行
        }
        System.out.println(name + "\t" + age);
    }
}
